<template>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'" destroy-on-close :close-on-click-modal="false"
        :before-close="closeDailog" :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="120px">
            <el-form-item label="职责类型" prop="type">
                <ec-dict-select dictType="resp_type" v-model="dataForm.type" placeholder="请选择职责类型"></ec-dict-select>
            </el-form-item>
            <el-form-item label="所属集团" prop="pkGroup">
                <ec-group-tree-select :visible="visible" v-model="dataForm.pkGroup" placeholder="所属集团">
                </ec-group-tree-select>
            </el-form-item>
            <el-form-item label="所属组织" prop="pkOrg">
                <ec-org-tree-select :visible="visible" :pkGroup="dataForm.pkGroup" v-model="dataForm.pkOrg"
                    placeholder="上级组织">
                </ec-org-tree-select>
            </el-form-item>
            <el-form-item label="职责名称" prop="name">
                <el-input v-model="dataForm.name" placeholder="职责名称"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="description">
                <el-input v-model="dataForm.description" placeholder="备注"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as responsibilityService from '@/api/upms/responsibility'
    import * as dictService from '@/api/bdms/dict'
    import * as orgService from '@/api/upms/org'
    import * as groupService from '@/api/upms/group'
    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    pkResponsibility: 0,
                    type: '',
                    pkGroup: '',
                    pkOrg: '',
                    name: '',
                    description: '',
                    revision: '',
                },
                dataRule: {
                    type: [{
                        required: true,
                        message: '职责类型不能为空',
                        trigger: 'blur'
                    }],
                    pkGroup: [{
                        required: true,
                        message: '所属集团不能为空',
                        trigger: 'change'
                    }],
                    pkOrg: [{
                        required: true,
                        message: '所属组织不能为空',
                        trigger: 'change'
                    }],
                    name: [{
                        required: true,
                        message: '职责名称不能为空',
                        trigger: 'blur'
                    }],
                    description: [{
                        required: true,
                        message: '备注不能为空',
                        trigger: 'blur'
                    }],
                },
                groupTree: [],
                orgsTree: [],
                groupTreeProps: {
                    parent: 'pkFathergroup',
                    value: 'pkGroup',
                    label: 'name',
                    children: 'children',
                },
                orgsTreeProps: {
                    parent: 'pkFatherorg',
                    value: 'pkOrg',
                    label: 'name',
                    children: 'children',
                },
            }
        },
        methods: {
            init(id) {
                this.dataForm.pkResponsibility = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.pkResponsibility) {
                        responsibilityService.getresponsibilityinfo(this.dataForm.pkResponsibility).then(
                            data => {
                                this.dataForm.type = data.type
                                this.dataForm.pkGroup = data.pkGroup
                                this.dataForm.pkOrg = data.pkOrg
                                this.dataForm.name = data.name
                                this.dataForm.description = data.description
                                this.dataForm.revision = data.revision
                            })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'pkResponsibility': this.dataForm.pkResponsibility || undefined,
                            'type': this.dataForm.type,
                            'pkGroup': this.dataForm.pkGroup,
                            'pkOrg': this.dataForm.pkOrg,
                            'name': this.dataForm.name,
                            'description': this.dataForm.description,
                            'revision': this.dataForm.revision,
                        }
                        var tick = !this.dataForm.pkResponsibility ? responsibilityService.addresponsibility(
                            params) : responsibilityService.updateresponsibility(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            },
        }
    }
</script>